#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char const *argv[])
{
        if(argc != 2){
                printf("./a.out file name\n");
                return -1;
        }

        int fd = open(argv[1], O_RDWR | O_CREAT, 0666);

        //写完之后 此时文件读写位置已经在文件末尾了
        write(fd,  "helloworld", sizeof("helloworld"));

        //需要移动读写位置,移动到开头
        lseek(fd, 0, SEEK_SET);

        char buf[1024] = {0};
        int ret =  read(fd, buf, sizeof(buf));

        if(ret > 0){
                write(STDOUT_FILENO, buf, ret);
        }

        close(fd);

        return 0;
}
